package com.jingxi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingxi.common.pojo.EasyUIDataGridResult;
import com.jingxi.common.pojo.IDUtils;
import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.mapper.TbItemMapper;
import com.jingxi.model.TbItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class TbItemServcieImpl implements TbItemServcie {
    @Autowired
    TbItemMapper tbItemMapper;
    @Override
    public TbItem getTbItemById(long id) {
        return tbItemMapper.selectByPrimaryKey(id);
    }

    /**
     *
     * @param page 第几页
     * @param rows 多少条记录
     * @return EasyUIDataGridResult包装记录数
     */
    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        EasyUIDataGridResult easyUIDataGridResult=new EasyUIDataGridResult();
        PageHelper.startPage(1,10);
        List<TbItem> li=tbItemMapper.selectList();
        PageInfo<TbItem> tol=new PageInfo<>(li);
        easyUIDataGridResult.setRows(li);
        easyUIDataGridResult.setTotal(tol.getTotal());
        return easyUIDataGridResult;
    }

    @Override
    public JingXiResult createItem(TbItem tbItem) {
        Date date=new Date();
        Long id= IDUtils.getItemId();
        tbItem.setId(id);
        tbItem.setStatus((byte) 1);
        tbItem.setCreated(date);
        tbItem.setUpdated(date);
        tbItemMapper.insert(tbItem);
        return JingXiResult.ok();
    }
}
